﻿Imports MySql.Data

Public Class MySQL
    Public Shared conn As New MySqlClient.MySqlConnection("Server=" & Config.MySQLServer & _
                                                          ";Database=" & Config.MySQLDatabase & _
                                                          ";Uid=" & Config.MySQLUsername & ";Pwd=" _
                                                          & Config.MySQLPassword & ";")
    Public Shared command As MySqlClient.MySqlCommand
    Public Shared dataReader As MySqlClient.MySqlDataReader
    Public Shared trans As MySqlClient.MySqlTransaction

    Public Shared Sub setUp()
        log.log(LogType.MSG_INFO)
        Console.Write("Loading MySQL...")
        conn.Open()
        conn.Close()
        Console.WriteLine("DONE")
    End Sub

    Public Shared Function testLogin(ByVal username As String, ByVal password As String, ByRef userid As Integer, ByRef online As Integer, ByRef accesslevel As Integer) As Boolean
        Try
            command = New MySqlClient.MySqlCommand("select * from accounts where username = '" & username & "' and password = '" & password & "'", conn)
            conn.Open()
            trans = conn.BeginTransaction()
            trans.Commit()
            dataReader = command.ExecuteReader()
            Do While (dataReader.Read())
                userid = dataReader.GetInt32("userid")
                online = dataReader.GetInt32("online")
                accesslevel = dataReader.GetInt32("accesslvl")
                conn.Close()
                Return True
            Loop
            conn.Close()
            userid = -1
            online = -1
            accesslevel = -1
            Return False
        Catch ex As MySqlClient.MySqlException
            Return False
        End Try
    End Function

    Public Shared Sub setOnline(ByVal userid As Integer, Optional ByVal val As Integer = 1)
        Try
            command = New MySqlClient.MySqlCommand("update accounts set online = '" & val & "' where userid = '" & userid & "'", conn)
            conn.Open()
            trans = conn.BeginTransaction()
            trans.Commit()
            command.ExecuteNonQuery()
            conn.Close()
        Catch ex As MySqlClient.MySqlException
        End Try
    End Sub

End Class
